﻿/***************************************************************************
 * 文件名：BankBusObjectFactory
 * 功能：
 * 说明：
 * -------------------------------------------------------------------------
 * 创建时间：2024/5/22 16:50:51
 * 创建人：林远
 * 邮箱： 17682102673@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using MySystem.Interface;
using System.Reflection;

namespace BankAccValidator.Server
{
    internal class BankBusObjectFactory
    {
        public static IBusPostParam CreateBusPostParam(Type type, string accountNumber, object criteria)
        {
            throw new NotImplementedException();
        }

        public static IBusPostParam CreateBusPostParam(Type type, string accountNumber, string iDNumber, object criteria)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 创建业务提交参数类实例
        /// </summary>
        /// <param name="type">业务提交参数类型</param>
        /// <param name="accountNumber">银行账户</param>
        /// <param name="iDNumber">身份证号</param>
        /// <param name="phoneNumber">预留电话号</param>
        /// <param name="criteria">其它信息</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="NullReferenceException"></exception>
        public static IBusPostParam CreateBusPostParam(Type type, string accountNumber, string iDNumber, string phoneNumber, object criteria)
        {
            //判断type是否继承业务参数接口（IBusPostParam）
            if (!type.IsSubclassOf(typeof(IBusPostParam)))
            {
                throw new ArgumentException($"错误的构造类型{type.Name},业务提交参数类必须继承接口{nameof(IBusPostParam)}！");
            }

            //获取业务类构造函数
            ConstructorInfo? construtor = null;
            IBusPostParam? result = null;
            object[] constructorParams = new object[] { accountNumber, iDNumber, phoneNumber, criteria };

            //获取四个构造函数的参数
            construtor = type.GetConstructors()
                .Where(p => !p.IsStatic && p.IsPublic
                && p.GetParameters().Length == 4 && p.GetParameters()[0].ParameterType == typeof(string) && p.GetParameters()[1].ParameterType == typeof(string) && p.GetParameters()[2].ParameterType == typeof(string))
                .FirstOrDefault();

            //如果未获取到构造函数
            if (construtor == null)
            {
                throw new NullReferenceException($@"未找到合适的业务提交参数类【{type.FullName}】构造方法！");
            }

            result = construtor.Invoke(constructorParams) as IBusPostParam;

            if (result == null)
            {
                throw new NullReferenceException($@"业务参数对象构造失败！");
            }

            return result;
        }
    }
}